#pragma once
#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define max_c 1024

using namespace std;

struct thread_data
{
    thread_data(int fd, sockaddr* saddr)
    : _fd(fd), _saddr(saddr)
    {}

    int _fd;
    sockaddr* _saddr;
};


class TCP_socket
{
    enum err
    {
        SOCKET_ERR = 1,
        BIND_ERR,
        LISTEN_ERR,
        CONNECT_ERR
    };
public:
    TCP_socket()
    {
        _fd = socket(AF_INET, SOCK_STREAM, 0);
        if (_fd < 0)
        {
            perror("socket");
            exit(SOCKET_ERR);
        }
    }

    //绑定socket
    bool Bind(const string& ip, const uint16_t& port)
    {
        sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = inet_addr(ip.c_str());
        addr.sin_port = htons(port);
        //socket长度
        socklen_t len = sizeof(addr);

        int ret = bind(_fd, reinterpret_cast<sockaddr*>(&addr), len);
        if (ret < 0)
        {
            perror("bind");
            exit(BIND_ERR);
            return false;
        }
        return true;
    }

    //设置为监听状态
    bool Listen()
    {
        if (listen(_fd, 50) < 0)
        {
            perror("listen");
            exit(LISTEN_ERR);
            return false;
        }
        return true;
    }

    //获取连接
    bool Accept_link(string& ip, uint16_t& port)
    {
        //接收链接
        sockaddr_in* acpt_addr = new sockaddr_in;
        socklen_t* len = new socklen_t;
        *len = sizeof(sockaddr_in);
        int acpt_fd = accept(_fd, (sockaddr*)acpt_addr, len);
        if (acpt_fd < 0)
        {
            perror("accept");
            return false;
        }

        //多线程处理
        pthread_t t;
        thread_data* td = new thread_data(acpt_fd, (sockaddr*)acpt_addr);
        pthread_create(&t, nullptr, thread_handler, td);

        return true;
    }

    //连接服务器
    int Connect(string& ip, uint16_t port)
    {
        sockaddr_in addr;
        addr.sin_addr.s_addr = inet_addr(ip.c_str());
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        socklen_t len = sizeof(addr);

       int ret = connect(_fd, (sockaddr*)&addr, len);
       if (ret <0)
        {
            perror("connect");
            exit(CONNECT_ERR);
        }
        return _fd;

    }

    ~TCP_socket()
    {
        close(_fd);
    }
public:
    //服务器处理
    void net_io(int fd, sockaddr* acpt_addr)
    {
        sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(acpt_addr);
        char buf[max_c];
        while(true)
        {
            //读取数据
            ssize_t readsize = read(fd, buf, max_c);
            if (readsize > 0)
            {
                //打印出读取的数据
                printf("[%s] [%d] [%d]:", inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), fd);
                cout << buf << endl;
            }
            else if (readsize == 0)
            {
                cout << "通信结束" << endl;
                break;
            }
            else if (readsize < 0)
            {
                perror("read");
            }            
        }
        close(fd);
    }

    //供线程用的
    static void* thread_handler(void* arg)
    {
        //分离线程
        pthread_detach(pthread_self());

        thread_data* td = reinterpret_cast<thread_data*>(arg);
        sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(td->_saddr);
        char buf[max_c];
        while(true)
        {
            //读取数据
            ssize_t readsize = read(td->_fd, buf, max_c);
            if (readsize > 0)
            {
                //打印出读取的数据
                printf("[%s] [%d] [%d]:", inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), td->_fd);
                cout << buf << endl;
            }
            else if (readsize == 0)
            {
                cout << "通信结束" << endl;
                break;
            }
            else if (readsize < 0)
            {
                perror("read");
            }            
        }
        close(td->_fd);
        return nullptr;
    }
private:
    int _fd;
};